package fun.codefarmer.pm.controller.management.emp;

import fun.codefarmer.pm.model.Employee;
import fun.codefarmer.pm.model.JobLevel;
import fun.codefarmer.pm.model.RespBean;
import fun.codefarmer.pm.model.RespPageBean;
import fun.codefarmer.pm.service.*;
import fun.codefarmer.pm.utils.POIUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @ ClassName EmpBasicController
 * @ Descriotion TODO
 * @ author  codeFarmer_z
 * @ 公众号：码农教程
 * @ Date 2022/5/21 21:43
 **/
@RequestMapping("/emp/basic")
@RestController
public class EmpBasicController {
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private NationService nationService;
    @Autowired
    private PoliticsstatusService politicsstatusService;
    @Autowired
    private JobLevelService jobLevelService;
    @Autowired
    PositionService positionService;
    @Autowired
    DepartmentService departmentService;

    /**
     * 员工基本信息 初始化查询
     * @param page
     * @param size
     * @param employee
     * @param beginDateScope
     * @return
     */
    @GetMapping("/")
    public RespPageBean getEmployeeByPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size, Employee employee, @RequestParam(required = false) Date[] beginDateScope) {
        System.out.println(employee);
        System.out.println(Arrays.toString(beginDateScope));
        return employeeService.getEmployeeByPage(page, size,employee,beginDateScope);
    }

    /**
     * 导入：分2步，
     * 1.先上传保存到服务器
     * 2.保存成功后再解析
     * @param file
     * @return
     */
    @PostMapping("/import")
    public RespBean importData(MultipartFile file) throws IOException {
        // 将文件命名为codefarmer.xls存在E盘
        //file.transferTo(new File("E:\\codefarmer.xls"));
        List<Employee> list = POIUtils.excel2Employee(file,
                // 查询全部民族，存民族id
                nationService.getAllNation(),
                //政治面貌
                politicsstatusService.getAllPoliticsstatus(),
                // 职位
                positionService.getAllPosition(),
                //
                departmentService.getAllDepartmentWithOutChildren(),
                // 职称
                (List<JobLevel>)jobLevelService.getAllJobLevel().getObj()
        );
        for (Employee employee : list) {
            System.out.println(employee.getPosid()+";");
        }
        int i = employeeService.addEmps(list);
        if (i == list.size()) {
            return RespBean.ok("上传成功");
        }
        return RespBean.error("上传失败");
    }
}
